<template>
  <div class="app-container">
    <!--        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
              <el-form-item label="风险单元名称" prop="ruName">
                <el-input
                  v-model="queryParams.ruName"
                  placeholder="请输入风险单元名称"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="生产场所单元" prop="ruProductName">
                <el-input
                  v-model="queryParams.ruProductName"
                  placeholder="请输入生产场所单元"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="生产过程反应条件" prop="ruCondition">
                <el-input
                  v-model="queryParams.ruCondition"
                  placeholder="请输入生产过程反应条件"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="贮罐区" prop="ruZhuguan">
                <el-input
                  v-model="queryParams.ruZhuguan"
                  placeholder="请输入贮罐区"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="库区" prop="ruKu">
                <el-input
                  v-model="queryParams.ruKu"
                  placeholder="请输入库区"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="危险化学品" prop="ruDangerChemicals">
                <el-input
                  v-model="queryParams.ruDangerChemicals"
                  placeholder="请输入危险化学品"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="风险特性" prop="ruRiskCs">
                <el-input
                  v-model="queryParams.ruRiskCs"
                  placeholder="请输入风险特性"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:unit:add']"
          icon="el-icon-plus"
          plain
          size="small"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:unit:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="small"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:unit:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="small"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['safe:unit:export']"
          :loading="exportLoading"
          icon="el-icon-download"
          plain
          size="small"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="unitList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <!--      <el-table-column align="center" label="风险单元ID" prop="ruId"/>-->
      <el-table-column :formatter="typeFormat" align="center" label="企业风险单元" prop="ruName"/>
      <el-table-column align="center" label="生产场所单元" prop="ruProductName"/>
      <el-table-column align="center" label="生产过程反应条件" prop="ruCondition">
      <template slot-scope="scope" v-if="scope.row.ruCondition">
        <div v-if="scope.row.ruCondition == 0">高温高压</div>
        <div v-if="scope.row.ruCondition == 1">易燃易爆</div>
        <div v-if="scope.row.ruCondition == 2">易发生化学物质泄漏</div>
      </template>
      </el-table-column>
      <el-table-column align="center" label="贮罐区" prop="ruZhuguan"/>
      <el-table-column align="center" label="库区" prop="ruKu"/>
      <el-table-column align="center" label="危险化学品" prop="ruDangerChemicals"/>
      <el-table-column align="center" label="风险特性" prop="ruRiskCs"/>
      <el-table-column align="center" label="风险源等级" prop="ruRiskSourceLv">
       <template slot-scope="scope" v-if="scope.row.ruRiskSourceLv">
          <div v-if="scope.row.ruRiskSourceLv == 0">一般风险源</div>
          <div v-if="scope.row.ruRiskSourceLv == 1">较重大风险源</div>
          <div v-if="scope.row.ruRiskSourceLv == 2">重大风险源</div>
        </template>
      </el-table-column>

      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['safe:unit:edit']"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['safe:unit:remove']"
            size="small"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改风险单元对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="60%">
      <el-form ref="form" :model="form" :rules="rules" class="clearfix  m-f-a-i" label-width="130px">
        <div class="content-left" style="width: 40%">
          <el-form-item label="企业风险单元" prop="ruName">
            <el-select v-model="form.ruName" placeholder="请选择">
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
            <el-input v-model="form.ruProductName" placeholder="请输入名称"/>
<!--            <el-input v-model="form.ruName" placeholder="请输入风险单元名称"/>-->
          </el-form-item>
<!--          <el-form-item  prop="ruProductName">

          </el-form-item>-->
          <el-form-item label="生产过程反应条件" prop="ruCondition">
            <div style="width: 100%;">
              <el-select v-model="rcIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="rcChange">
                <el-option v-for="item in rc" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item label="危险化学品" prop="ruDangerChemicals">
            <el-input v-model="form.ruDangerChemicals" placeholder="请输入危险化学品"/>
          </el-form-item>
        </div>
        <div class="content-left" style="float: right; width: 45%">
<!--          <el-form-item label="贮罐区" prop="ruZhuguan">
            <el-input v-model="form.ruZhuguan" placeholder="请输入贮罐区"/>
          </el-form-item>
          <el-form-item label="库区" prop="ruKu">
            <el-input v-model="form.ruKu" placeholder="请输入库区"/>
          </el-form-item>-->
          <el-form-item label="风险特性" prop="ruRiskCs">
            <el-input v-model="form.ruRiskCs" placeholder="请输入风险特性"/>
          </el-form-item>
          <el-form-item label="风险源等级" prop="ruRiskSourceLv">
            <div style="width: 100%;">
              <el-select v-model="rsvIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="rsvChange">
                <el-option v-for="item in rsv" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addUnit, delUnit, exportUnit, getUnit, listUnit, updateUnit} from "@/api/safe/unit";
import {InfoItem} from "@/api/enterprise/info";
import {isIntegerNotMust,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Unit",
  data() {
    return {
      rc: [
        {
          value: '0',
          label: '高温高压'
        },
        {
          value: '1',
          label: '易燃易爆'
        },
        {
          value: '2',
          label: '易发生化学物质泄漏'
        },
        {
          value: '3',
          label: '其他'
        }
      ],
      rcIndex: '0',

      rsv: [
        {
          value: '0',
          label: '一般风险源'
        },
        {
          value: '1',
          label: '较重大风险源'
        },
        {
          value: '2',
          label: '重大风险源'
        }
      ],
      rsvIndex: '0',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 风险单元表格数据
      unitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ruName: null,
        ruProductName: null,
        ruCondition: null,
        ruZhuguan: null,
        ruKu: null,
        ruDangerChemicals: null,
        ruRiskCs: null,
        ruRiskSourceLv: null,
        epId:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ruName: [
          {required: true, message: "风险单元名称不能为空", trigger: "blur"}
        ],
        ruProductName: [
          {required: true, message: "生产场所单元不能为空", trigger: "blur"}
        ],
      },
      typeOptions:[]
    };
  },
  created() {
    this.getDicts("qyfxdy").then(response => {
      this.typeOptions = response.data;
    });
    this.getList();
  },
  mounted() {

  },
  methods: {
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.ruName);
    },
    rcChange(e) {
      this.form.ruCondition = e;
    },
    rsvChange(e) {
      this.form.ruRiskSourceLv = e;
    },
    /** 查询风险单元列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listUnit(this.queryParams).then(response => {
            this.unitList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        epId:null,
        ruId: null,
        ruName: null,
        ruProductName: null,
        ruCondition: null,
        ruZhuguan: null,
        ruKu: null,
        ruDangerChemicals: null,
        ruRiskCs: null,
        ruRiskSourceLv: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ruId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加风险单元";
      this.form.ruRiskSourceLv=this.rsv[this.rsvIndex].value
      this.form.ruCondition= this.rc[this.rcIndex].value
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ruId = row.ruId || this.ids
      getUnit(ruId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改风险单元";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ruId != null) {
            updateUnit(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              console.log(this.form);
              addUnit(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ruIds = row.ruId || this.ids;
      this.$confirm('是否确认删除风险单元编号为"' + ruIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delUnit(ruIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有风险单元数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportUnit(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
